/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pki;

import java.security.cert.CRL;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Date;
import org.bouncycastle.asn1.x509.KeyUsage;

/**
 *
 * @author Gaetan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        
        //Certificat top level
        CertificateTools ct = new CertificateTools();
        //X509Certificate certifTopLevel = (X509Certificate) ct.loadCertificateFile("W:/PKISecurity/netbeans_project/pki/top_level.cer").get(0);
        
        //System.out.println(CertificateTools.getInfoCertificate(certifTopLevel));
        
        //ctTopLevel.saveCertificate(certifTopLevel, "W:/PKISecurity/netbeans_project/pki/test.cer");
        
        //Générer certificat signé par top level
        Calendar calendar = Calendar.getInstance();
        Date notBefore = calendar.getTime();
        calendar.set(2012, 12, 31);
        Date notAfter = calendar.getTime();
        
        //KeyTools ktCertif = new KeyTools("DSA");
        KeyUsage keyUsage = new KeyUsage(KeyUsage.digitalSignature);

        

        /*X509Certificate certif = ct.generateCertificate(notBefore, notAfter, ktCertif.getKeyPair(), keyUsage, "CN=ZOB");
        
        ct.saveX509Certificate(certif, "C:/Documents and Settings/piK/Documents/workspace-sts-2.8.1.RELEASE/netbeans_project/pki/certificates/pasmerdier.cer");*/
        //ctTopLevel.saveAllCertificates(certif, "W:/PKISecurity/netbeans_project/pki/merdier.cer");
        
        //KeyPair kpv3 = kpt.generateKeyPair("RSA");
        //X509Certificate v3 = ct.generateCertificate(kpt.getPrivateKey(kpv1), v1, kpv3, "SHA1withRSA");
        
        //System.out.println(CertificateTools.getInfoCertificate(v3));
        
        //ct.saveCertificate(v3, "W:/PKISecurity/netbeans_project/pki/v3.cer");
        
        PKIService s = new PKIService();
        s.createCertificate("RSA", notBefore, notAfter, keyUsage, "sign2", "CN=Zbo", "zbo", "zbo", "nszabo2@gmail.com");
        //System.out.println(s.getAllCertificates("zbo", "zbo").size());
        
        
        s.writeX509CertificateFile("zbo", "zbo", "sign2");
        s.writePKCS12CertificateFile("zbo", "zbo", "sign2");
        
        X509Certificate x = s.getX509Certificate("zbo", "zbo", "sign2");
        s.writeKeyFile("zbo", "zbo", "sign2");
        System.out.println(s.isRevoked("zbo", x));
        s.revoke("zbo", x, 1);
        System.out.println(s.isRevoked("zbo", x));
    }
}
